<?php

namespace app\api\model\music;

use app\logic\model\music\MusicDjTeachingLogicModel;
use app\logic\model\music\MusicLogicModel;
use app\logic\model\user\UserLogicModel;
use app\logic\single\GoodsSingle;
use app\logic\single\MusicSingle;

class MusicDjTeachingApiModel extends MusicDjTeachingLogicModel
{

    public function getApiList(array $param,$where=[],$field="*",$page=1,$limit=15,$order="id desc"){

        $query=$this->where($where)
            ->where(function ($q) use ($param){
                $ids_where=[];
                $ids_check=-1;
                if(check_request($param,'title')){
                    $music_title = MusicLogicModel::getIdsByTitle($param['title']);
                    if(!empty($music_title)){
                        $ids_check = 1;
                        $ids_where = $music_title;
                    }
                }
                if(check_request($param,'writer_name')){
                    if($ids_check == 1){
                        $user_id_arr =UserLogicModel::getIdsByWriterName($param['writer_name']);
                        $music_writer_name = MusicLogicModel::getIdsByUserId($user_id_arr);
                        if(empty($music_writer_name)){
                            $ids_check = -1;
                        }else{
                            $ids_check = 1;
                            $ids_where = array_merge($ids_where,$music_writer_name);
                        }

                    }
                }

                if($ids_check == 1){
                    $q->whereIn("music_id",array_unique($ids_where));
                }
            })
            ->with(['music'])->visible(
                ['music'=>['title','url']]
            );
        $list = $query->field($field)->order($order)
            ->page($page,$limit)->select();
        $count = $query->count();
        if($count>0){
            $list = $this->getWriterByMusic($list->toArray());
        }
        return [$list,$count];
    }

    public function details($id, string $field='*')
    {
        $info = $this->getFindById($id,$field);
        if(empty($info)){
            return false;
        }
        [$info['music'],$info['writer']] = MusicSingle::getInstance()->getMusicAndUser($info['music_id']);

        $info['is_collection'] = GoodsSingle::getInstance()->isCollection($id,30);
        return $info;
    }
}